package crp.core.portal.domain.repositories;

import com.qwlabs.lang.C2;
import crp.core.portal.domain.models.PortalSectionEntity;
import crp.core.shared.LifecycleStatus;
import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import io.quarkus.panache.common.Parameters;
import io.quarkus.panache.common.Sort;

import javax.enterprise.context.ApplicationScoped;
import java.util.Set;
import java.util.stream.Stream;

@ApplicationScoped
public class PortalSectionRepository implements PanacheRepositoryBase<PortalSectionEntity, String> {

    public Stream<PortalSectionEntity> findOrderedAll() {
        return findAll(Sort.ascending("orderNumber")).stream();
    }

    public Stream<PortalSectionEntity> findOrderedByStatus(LifecycleStatus... statuses) {
        if (C2.isEmpty(statuses)) {
            return Stream.empty();
        }
        return find("status in (:statuses)",
                Sort.ascending("orderNumber"),
                Parameters.with("statuses", C2.set(statuses)))
                .stream();
    }

    public Integer nextOrderNumber() {
        return findAll(Sort.descending("orderNumber"))
                .firstResultOptional()
                .map(PortalSectionEntity::getOrderNumber)
                .orElse(0) + 1;
    }

    public Stream<PortalSectionEntity> findByIds(Set<String> ids) {
        return null;
    }
}
